package gui.addrecipe;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;

import start.Start;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Bewertung eines Gerichts durch 5-Sterne-System.
 * Durch Ueberfahren mit der Maus werden die Sterne gesetzt. Mit einem Klick werden sie gemerkt.
 * ( = Bewertung gesetzt)
 * @author Dominik Korittki
 */
public class Stars extends JPanel {
	
	/** Leerer Stern 1 */
	private JLabel lblEmptyStar1 = new JLabel(new ImageIcon(Start.getImgs().getSternLeer()));
	
	/** Leerer Stern 2 */
	private JLabel lblEmptyStar2 = new JLabel(new ImageIcon(Start.getImgs().getSternLeer()));
	
	/** Leerer Stern 3 */
	private JLabel lblEmptyStar3 = new JLabel(new ImageIcon(Start.getImgs().getSternLeer()));
	
	/** Leerer Stern 4 */
	private JLabel lblEmptyStar4 = new JLabel(new ImageIcon(Start.getImgs().getSternLeer()));
	
	/** Leerer Stern 5 */
	private JLabel lblEmptyStar5 = new JLabel(new ImageIcon(Start.getImgs().getSternLeer()));
	
	/** Voller Stern 1 */
	private JLabel lblFullStar1 = new JLabel(new ImageIcon(Start.getImgs().getSternVoll()));
	
	/** Voller Stern 2 */
	private JLabel lblFullStar2 = new JLabel(new ImageIcon(Start.getImgs().getSternVoll()));
	
	/** Voller Stern 3 */
	private JLabel lblFullStar3 = new JLabel(new ImageIcon(Start.getImgs().getSternVoll()));
	
	/** Voller Stern 4 */
	private JLabel lblFullStar4 = new JLabel(new ImageIcon(Start.getImgs().getSternVoll()));
	
	/** Voller Stern 5 */
	private JLabel lblFullStar5 = new JLabel(new ImageIcon(Start.getImgs().getSternVoll()));
	
	/** Position Stern 1 */
	private GridBagConstraints starPos1;
	
	/** Position Stern 2 */
	private GridBagConstraints starPos2;
	
	/** Position Stern 3 */
	private GridBagConstraints starPos3;
	
	/** Position Stern 4 */
	private GridBagConstraints starPos4;
	
	/** Position Stern 5 */
	private GridBagConstraints starPos5;

	/** Serial Version ID */
	private static final long serialVersionUID = 1L;
	
	/** True falls eine Bewertung gesetzt (angeklickt) wurde */
	private boolean pressed = false;

	/**
	 * Erstellt das Panel.
	 */
	public Stars() {		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		//GridBagPosition der Sterne festlegen
		GridBagConstraints invLabelPos = new GridBagConstraints();
		invLabelPos.gridx = 0;
		invLabelPos.gridy = 0;
		
		starPos1 = new GridBagConstraints();
		starPos1.gridx = 1;
		starPos1.gridy = 0;
		
		starPos2 = new GridBagConstraints();
		starPos2.gridx = 2;
		starPos2.gridy = 0;
		
		starPos3 = new GridBagConstraints();
		starPos3.gridx = 3;
		starPos3.gridy = 0;
		
		starPos4 = new GridBagConstraints();
		starPos4.gridx = 4;
		starPos4.gridy = 0;
		
		starPos5 = new GridBagConstraints();
		starPos5.gridx = 5;
		starPos5.gridy = 0;
		
		//MouseListener		
		lblEmptyStar1.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {}
			
			@Override
			public void mouseExited(MouseEvent e) {}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithFullStar(0);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblEmptyStar2.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {}
			
			@Override
			public void mouseExited(MouseEvent e) {}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithFullStar(1);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblEmptyStar3.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {}
			
			@Override
			public void mouseExited(MouseEvent e) {}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithFullStar(2);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblEmptyStar4.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {}
			
			@Override
			public void mouseExited(MouseEvent e) {}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithFullStar(3);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblEmptyStar5.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {}
			
			@Override
			public void mouseExited(MouseEvent e) {}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithFullStar(4);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblFullStar1.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {
				pressed = true;
				((AddRecipeRightBottomPanel) getParent()).setBewertung(1);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(-1);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(0);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblFullStar2.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {
				pressed = true;
				((AddRecipeRightBottomPanel) getParent()).setBewertung(2);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(-1);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(1);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblFullStar3.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {
				pressed = true;
				((AddRecipeRightBottomPanel) getParent()).setBewertung(3);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(-1);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(2);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblFullStar4.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {
				pressed = true;
				((AddRecipeRightBottomPanel) getParent()).setBewertung(4);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(-1);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(3);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		lblFullStar5.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			
			@Override
			public void mousePressed(MouseEvent e) {
				pressed = true;
				((AddRecipeRightBottomPanel) getParent()).setBewertung(5);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				if(!pressed) replaceWithEmptyStar(-1);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {}
			
			@Override
			public void mouseClicked(MouseEvent e) {}
		});
		
		//Button zum Zuruecksetzen
		JButton btnReset = new JButton(new ImageIcon(Start.getImgs().getMinusSmall()));
		btnReset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				reset();
			}
		});
		btnReset.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) 		
					reset();
			}
			@Override public void keyReleased(KeyEvent e) {}
			@Override public void keyTyped(KeyEvent e) {}
		});
		GridBagConstraints gbc_btnReset = new GridBagConstraints();
		gbc_btnReset.gridx = 6;
		gbc_btnReset.gridy = 0;
		gbc_btnReset.insets = new Insets(0, 10, 0, 0);
		
		add(lblEmptyStar1, starPos1);
		add(lblEmptyStar2, starPos2);
		add(lblEmptyStar3, starPos3);
		add(lblEmptyStar4, starPos4);
		add(lblEmptyStar5, starPos5);
		add(btnReset, gbc_btnReset);
	}
	
	/**
	 * Erstellt ein Sternsystem, welches nicht interaktiv ist. Wird zur Anzeige von eingetragenen Rezepten gebraucht
	 * @param i Anzahl gesetzter Sterne
	 */
	public Stars(int i) {
		
		//GridBagPosition der Sterne festlegen
				GridBagConstraints invLabelPos = new GridBagConstraints();
				invLabelPos.gridx = 0;
				invLabelPos.gridy = 0;
				
				starPos1 = new GridBagConstraints();
				starPos1.gridx = 1;
				starPos1.gridy = 0;
				
				starPos2 = new GridBagConstraints();
				starPos2.gridx = 2;
				starPos2.gridy = 0;
				
				starPos3 = new GridBagConstraints();
				starPos3.gridx = 3;
				starPos3.gridy = 0;
				
				starPos4 = new GridBagConstraints();
				starPos4.gridx = 4;
				starPos4.gridy = 0;
				
				starPos5 = new GridBagConstraints();
				starPos5.gridx = 5;
				starPos5.gridy = 0;
		
		switch (i) {
		
		case 1:
			add(lblFullStar1, starPos1);
			add(lblEmptyStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 2:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 3:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 4:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblFullStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 5:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblFullStar4, starPos4);
			add(lblFullStar5, starPos5);
			break;

		default:
			add(lblEmptyStar1, starPos1);
			add(lblEmptyStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		}
	}
	
	/**
	 * Ersetzt leere Sterne mit vollen
	 * @param i Anzahl voller Sterne - 1
	 */
	private void replaceWithFullStar(int i) {
		switch (i) {
		case 0:
			remove(lblEmptyStar1);
			revalidate();
			repaint();
			add(lblFullStar1, starPos1);
			break;
			
		case 1:
			remove(lblEmptyStar1);
			remove(lblEmptyStar2);
			revalidate();
			repaint();
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			break;
			
		case 2:
			remove(lblEmptyStar1);
			remove(lblEmptyStar2);
			remove(lblEmptyStar3);
			revalidate();
			repaint();
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			break;
		
		case 3:
			remove(lblEmptyStar1);
			remove(lblEmptyStar2);
			remove(lblEmptyStar3);
			remove(lblEmptyStar4);
			revalidate();
			repaint();
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblFullStar4, starPos4);
			break;
			
		case 4:
			remove(lblEmptyStar1);
			remove(lblEmptyStar2);
			remove(lblEmptyStar3);
			remove(lblEmptyStar4);
			remove(lblEmptyStar5);
			revalidate();
			repaint();
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblFullStar4, starPos4);
			add(lblFullStar5, starPos5);
			break;

		default:
			break;
		}
	}
	
	/**
	 * Ersetzt volle Sterne mit leeren
	 * @param i Anzahl voller Sterne + 1
	 */
	private void replaceWithEmptyStar(int i) {
		switch (i) {
		case 0:
			remove(lblFullStar2);
			remove(lblFullStar3);
			remove(lblFullStar4);
			remove(lblFullStar5);
			revalidate();
			repaint();
			add(lblEmptyStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
			
		case 1:
			remove(lblFullStar3);
			remove(lblFullStar4);
			remove(lblFullStar5);
			revalidate();
			repaint();
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
			
		case 2:
			remove(lblFullStar4);
			remove(lblFullStar5);
			revalidate();
			repaint();
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		
		case 3:
			remove(lblFullStar5);
			revalidate();
			repaint();
			add(lblEmptyStar5, starPos5);
			break;
			
		case -1:
			remove(lblFullStar1);
			remove(lblFullStar2);
			remove(lblFullStar3);
			remove(lblFullStar4);
			remove(lblFullStar5);
			revalidate();
			repaint();
			add(lblEmptyStar1, starPos1);
			add(lblEmptyStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
			
		default:
			break;
		}
	}
	
	/**
	 * Alle gesetzten Sterne wieder loeschen
	 */
	public void reset() {
		replaceWithEmptyStar(-1);
		pressed = false;
		((AddRecipeRightBottomPanel) getParent()).setBewertung(0);
	}
	
	/**
	 * Setzt Sterne, wenn auf sie geklickt wurde
	 * @param i Anzahl gesetzter Sterne
	 */
	public void setStars(int i) {
		pressed = true;
		remove(lblEmptyStar1);
		remove(lblEmptyStar2);
		remove(lblEmptyStar3);
		remove(lblEmptyStar4);
		remove(lblEmptyStar5);
		remove(lblFullStar1);
		remove(lblFullStar2);
		remove(lblFullStar3);
		remove(lblFullStar4);
		remove(lblFullStar5);
		revalidate();
		repaint();
		
		switch (i) {
		case 1:
			add(lblFullStar1, starPos1);
			add(lblEmptyStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 2:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 3:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 4:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblFullStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		case 5:
			add(lblFullStar1, starPos1);
			add(lblFullStar2, starPos2);
			add(lblFullStar3, starPos3);
			add(lblFullStar4, starPos4);
			add(lblFullStar5, starPos5);
			break;

		default:
			add(lblEmptyStar1, starPos1);
			add(lblEmptyStar2, starPos2);
			add(lblEmptyStar3, starPos3);
			add(lblEmptyStar4, starPos4);
			add(lblEmptyStar5, starPos5);
			break;
		}
	}

}
